package cn.test;

import org.junit.Test;

import cn.TcpUtil.TcpUtil;
import cn.tcpTask.SwitchTask;
import cn.tcpTask.SwitchTaskRunner;

import com.serotonin.modbus4j.exception.ModbusTransportException;
import com.serotonin.modbus4j.msg.ModbusRequest;
import com.serotonin.modbus4j.msg.ModbusResponse;
import com.serotonin.modbus4j.msg.ReadHoldingRegistersRequest;

public class TcpUtilTest2 {

	public static void main(String[] args) throws ModbusTransportException {
			SwitchTask st=new SwitchTask("", 3, 161, true, 3000);
			SwitchTask st1=new SwitchTask("", 3, 162, false, 5000);
			SwitchTask st2=new SwitchTask("", 3, 163, true, 8000);
			SwitchTask st3=new SwitchTask("", 3, 164, true, 3000);
			SwitchTask[] sts=new SwitchTask[]{st,st1,st2,st3};
			SwitchTaskRunner str=new SwitchTaskRunner(sts);
			Thread t=new Thread(str);
            t.start();
			System.out.println("hahaahahah");
		
	}
	
	@Test
	public  void test1() throws ModbusTransportException {
	       TcpUtil tu= TcpUtil.startTcpService();
			for (int i = 0; i < 1000; i++) {
				long before=System.currentTimeMillis();
				//System.out.println("aaa的第"+i+"次请求#######");
				ModbusRequest modbusRequest = new ReadHoldingRegistersRequest(2, 0,20);
				ModbusResponse response = tu.sendRequest(modbusRequest);
//				if (response != null) {
//					ByteQueue queue = new ByteQueue();
//					response.write(queue);
//					Sensor sensor = ByteQueueUtil.ByteQueueToSensor(queue
//							.toString());
//					System.out.println("sensor" + sensor);
//				}
				long after=System.currentTimeMillis()-before;
				System.out.println("aaa的第"+i+"次请求#######"+after);
				//System.out.println("#########请求完成#########"+after);
			}
		}
	@Test
	public  void test2() throws ModbusTransportException {
	       TcpUtil tu= TcpUtil.startTcpService();
			for (int i = 0; i < 1000; i++) {
				long before=System.currentTimeMillis();
				//System.out.println("bbb的第"+i+"次请求#######");
				ModbusRequest modbusRequest = new ReadHoldingRegistersRequest(2, 0,20);
				ModbusResponse response = tu.sendRequest(modbusRequest);
//				if (response != null) {
//					ByteQueue queue = new ByteQueue();
//					response.write(queue);
//					Sensor sensor = ByteQueueUtil.ByteQueueToSensor(queue
//							.toString());
//					System.out.println("sensor" + sensor);
//				}
				long after=System.currentTimeMillis()-before;
				System.out.println("bbb的第"+i+"次请求#######"+after);
				//System.out.println("#########请求完成#########"+after);
			}
		}
	@Test
	public  void test4() throws ModbusTransportException {
	       TcpUtil tu= TcpUtil.startTcpService();
			for (int i = 0; i < 1000; i++) {
				long before=System.currentTimeMillis();
			//	System.out.println("ccc的第"+i+"次请求#######");
				ModbusRequest modbusRequest = new ReadHoldingRegistersRequest(2, 0,20);
				ModbusResponse response = tu.sendRequest(modbusRequest);
//				if (response != null) {
//					ByteQueue queue = new ByteQueue();
//					response.write(queue);
//					Sensor sensor = ByteQueueUtil.ByteQueueToSensor(queue
//							.toString());
//					System.out.println("sensor" + sensor);
//				}
				long after=System.currentTimeMillis()-before;
				System.out.println("ccc的第"+i+"次请求#######"+after);	
		//		System.out.println("#########请求完成#########"+after);
			}
		}
	@Test
	public  void test5() throws ModbusTransportException {
	       TcpUtil tu= TcpUtil.startTcpService();
			for (int i = 0; i < 1000; i++) {
				long before=System.currentTimeMillis();
				//System.out.println("ddd的第"+i+"次请求#######");
				ModbusRequest modbusRequest = new ReadHoldingRegistersRequest(2, 0,20);
				tu.sendRequest(modbusRequest);
//				if (response != null) {
//					ByteQueue queue = new ByteQueue();
//					response.write(queue);
//					Sensor sensor = ByteQueueUtil.ByteQueueToSensor(queue
//							.toString());
//					System.out.println("sensor" + sensor);
//				}
				long after=System.currentTimeMillis()-before;
				System.out.println("ddd的第"+i+"次请求#######"+after);
				//System.out.println("#########请求完成#########"+after);
			}
		}
}
